#####################################MySQL#############################################
cat>>/usr/lib/systemd/system/mariadb.service<<EOF
LimitNOFILE=65535
LimitNPROC=65535
EOF
echo "#Set MySQL Max Connect For OpenStack At:$(date +%Y-%m-%d)" >> /etc/rc.d/rc.local
echo '/usr/bin/mysql -uroot -pmysql_root_pwd -e "set global max_connections=65535;"'>> /etc/rc.d/rc.local
systemctl restart mariadb
/usr/bin/mysql -uroot -pmysql_root_pwd -e "set global max_connections=65535;"

#####################################keystone#############################################
ADMIN_TOKEN=$(openssl rand -hex 10)
#echo $ADMIN_TOKEN
openstack-config --set /etc/keystone/keystone.conf DEFAULT admin_token $ADMIN_TOKEN

openstack-config --set /etc/keystone/keystone.conf sql connection mysql://mysq_keystone_user:mysql_keystone_pwd@controller/keystone
openstack-config --set /etc/keystone/keystone.conf DEFAULT debug True
openstack-config --set /etc/keystone/keystone.conf DEFAULT verbose True
openstack-config --set /etc/keystone/keystone.conf token provider keystone.token.providers.uuid.Provider
openstack-config --set /etc/keystone/keystone.conf token driver keystone.token.persistence.backends.sql.Token

keystone-manage pki_setup --keystone-user keystone --keystone-group keystone
chown -R keystone:keystone /var/log/keystone
chown -R keystone:keystone /etc/keystone/ssl
chmod -R o-rwx /etc/keystone/ssl

mysql -uroot -pmysql_root_pwd  -e "CREATE DATABASE keystone;"
mysql -uroot -pmysql_root_pwd  -e "GRANT ALL PRIVILEGES ON keystone.* TO 'mysq_keystone_user'@'localhost' IDENTIFIED BY 'mysql_keystone_pwd';"
mysql -uroot -pmysql_root_pwd  -e "GRANT ALL PRIVILEGES ON keystone.* TO 'mysq_keystone_user'@'controller' IDENTIFIED BY 'mysql_keystone_pwd';"
mysql -uroot -pmysql_root_pwd  -e "GRANT ALL PRIVILEGES ON keystone.* TO 'mysq_keystone_user'@'%' IDENTIFIED BY 'mysql_keystone_pwd';"

su -s /bin/sh -c "keystone-manage db_sync" 

systemctl enable openstack-keystone.service
systemctl start openstack-keystone.service

export OS_SERVICE_TOKEN=`echo $ADMIN_TOKEN`
export OS_SERVICE_ENDPOINT=http://controlle:35357/v2.0

keystone tenant-create --name=admin_tenant --description="Admin Tenant"
keystone tenant-create --name=service_tenant --description="Service Tenant"

keystone user-create --name=admin_user --pass=admin_pwd --email=admin_email
keystone role-create --name=admin_role

keystone user-role-add --user=admin_user --tenant=admin_tenant --role=admin_role

keystone service-create --name=keystone --type=identity --description="Keystone Identity Service"

keystone role-create --name=_member_
keystone user-role-add --tenant admin_tenant --user admin_user --role _member_
keystone tenant-create --name demo_tenant --description "Demo Tenant"
keystone user-create --name demo_user --pass demo_pwd  --email demo_email 
keystone user-role-add --tenant demo_tenant --user demo_user --role _member_

keystone endpoint-create \
--service-id=$(keystone service-list | awk '/ identity / {print $2}') \
--publicurl=http://controller:5000/v2.0 \
--internalurl=http://controller:5000/v2.0 \
--adminurl=http://controller:35357/v2.0

cat>>~/keystonerc_admin<<EOF
export OS_USERNAME=admin_user
export OS_PASSWORD=admin_pwd
export OS_TENANT_NAME=admin_tenant
export OS_AUTH_URL=http://controller:35357/v2.0
EOF

unset OS_SERVICE_TOKEN OS_SERVICE_ENDPOINT

#############################glance######################################

source ~/keystonerc_admin

openstack-config --set /etc/glance/glance-api.conf DEFAULT sql_connection mysql://mysql_glance_user:mysql_glance_pwd@controller/glance
openstack-config --set /etc/glance/glance-registry.conf DEFAULT sql_connection mysql://mysql_glance_user:mysql_glance_pwd@controller/glance

mysql -uroot -pmysql_root_pwd -e "CREATE DATABASE glance;"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON glance.* TO 'mysql_glance_user'@'localhost' IDENTIFIED BY 'mysql_glance_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON glance.* TO 'mysql_glance_user'@'controller' IDENTIFIED BY 'mysql_glance_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON glance.* TO 'mysql_glance_user'@'%' IDENTIFIED BY 'mysql_glance_pwd';"

keystone user-create --name=glance_user --pass=glance_pwd --email=galnce_email
keystone user-role-add --user=glance_user --tenant=service_tenant --role=admin_tenant

keystone service-create --name=glance --type=image --description="Glance Image Service"

keystone endpoint-create \
--service-id=$(keystone service-list | awk '/ image / {print $2}')  \
--publicurl=http://controller:9292 \
--internalurl=http://controller:9292 \
--adminurl=http://controller:9292

openstack-config --set /etc/glance/glance-api.conf DEFAULT debug True
openstack-config --set /etc/glance/glance-api.conf DEFAULT verbose True
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_host controller
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_port 35357
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_protocol http
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken admin_tenant_name service
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken admin_user glance_user
openstack-config --set /etc/glance/glance-api.conf keystone_authtoken admin_password glance_pwd
openstack-config --set /etc/glance/glance-api.conf paste_deploy flavor keystone

openstack-config --set /etc/glance/glance-registry.conf DEFAULT debug True
openstack-config --set /etc/glance/glance-registry.conf DEFAULT verbose True
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_host controller
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_port 35357
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_protocol http
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken admin_tenant_name service
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken admin_user glance_user
openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken admin_password glance_pwd
openstack-config --set /etc/glance/glance-registry.conf paste_deploy flavor keystone

su -s /bin/sh -c "glance-manage db_sync"
chown -R glance:glance /var/log/glance/api.log

systemctl enable openstack-glance-api.service openstack-glance-registry.service
systemctl start openstack-glance-api.service openstack-glance-registry.service
systemctl restart openstack-glance-api.service openstack-glance-registry.service

###########################nova#################################

keystone user-create --name=nova_user --pass=nova_pwd --email=nova_email
keystone user-role-add --user=nova_user --tenant=service_tenant --role=admin_role

keystone service-create --name=nova --type=compute --description="Nova Compute Service"

keystone endpoint-create \
--service-id=$(keystone service-list | awk '/ compute / {print $2}') \
--publicurl=http://controller:8774/v2/%\(tenant_id\)s \
--internalurl=http://controller:8774/v2/%\(tenant_id\)s \
--adminurl=http://controller:8774/v2/%\(tenant_id\)s

openstack-config --set /etc/nova/nova.conf database connection mysql://mysql_nova_user:mysql_nova_pwd@controller/nova

mysql -uroot -pmysql_root_pwd -e "CREATE DATABASE nova;"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON nova.* TO 'mysql_nova_user'@'localhost' IDENTIFIED BY 'mysql_nova_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON nova.* TO 'mysql_nova_user'@'controller' IDENTIFIED BY 'mysql_nova_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON nova.* TO 'mysql_nova_user'@'%' IDENTIFIED BY 'mysql_nova_pwd';"

openstack-config --set /etc/nova/nova.conf DEFAULT debug True
openstack-config --set /etc/nova/nova.conf DEFAULT verbose True


openstack-config --set /etc/nova/nova.conf DEFAULT rpc_backend rabbit 
openstack-config --set /etc/nova/nova.conf DEFAULT rabbit_host controller
openstack-config --set /etc/nova/nova.conf DEFAULT rabbit_password rabbitmq_pwd


openstack-config --set /etc/nova/nova.conf DEFAULT my_ip MYIP
openstack-config --set /etc/nova/nova.conf DEFAULT vncserver_listen MYIP
openstack-config --set /etc/nova/nova.conf DEFAULT vncserver_proxyclient_address MYIP


openstack-config --set /etc/nova/nova.conf DEFAULT auth_strategy keystone
openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_host controller
openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_protocol http
openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_port 35357
openstack-config --set /etc/nova/nova.conf keystone_authtoken admin_user nova_user
openstack-config --set /etc/nova/nova.conf keystone_authtoken admin_tenant_name service
openstack-config --set /etc/nova/nova.conf keystone_authtoken admin_password nova_pwd

openstack-config --set /etc/nova/api-paste.ini filter:authtoken paste.filter_factory keystoneclient.middleware.auth_token:filter_factory
openstack-config --set /etc/nova/api-paste.ini filter:authtoken auth_host controller
openstack-config --set /etc/nova/api-paste.ini filter:authtoken admin_tenant_name service
openstack-config --set /etc/nova/api-paste.ini filter:authtoken admin_user nova_user
openstack-config --set /etc/nova/api-paste.ini filter:authtoken admin_password nova_pwd

su -s /bin/sh -c "nova-manage db sync"

systemctl enable openstack-nova-api.service openstack-nova-cert.service \
openstack-nova-consoleauth.service openstack-nova-scheduler.service \
openstack-nova-conductor.service openstack-nova-novncproxy.service
  
systemctl start openstack-nova-api.service openstack-nova-cert.service \
openstack-nova-consoleauth.service openstack-nova-scheduler.service \
openstack-nova-conductor.service openstack-nova-novncproxy.service

#############################neutron#####################################

keystone user-create --name neutron_user --pass neutron_pwd --email neutron_email
keystone user-role-add --user neutron_user --tenant service_tenant --role admin_role
keystone service-create --name neutron --type network --description "OpenStack Networking"
keystone endpoint-create \
--service-id $(keystone service-list | awk '/ network / {print $2}') \
--publicurl http://controller:9696 \
--adminurl http://controller:9696 \
--internalurl http://controller:9696

mysql -uroot -pmysql_root_pwd -e "CREATE DATABASE neutron;"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON neutron.* TO 'mysql_neutron_user'@'localhost' IDENTIFIED BY 'mysql_neutron_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON neutron.* TO 'mysql_neutron_user'@'%' IDENTIFIED BY 'mysql_neutron_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON neutron.* TO 'mysql_neutron_user'@'controller' IDENTIFIED BY 'mysql_neutron_pwd';"

openstack-config --set /etc/neutron/neutron.conf database connection mysql://mysql_neutron_user:mysql_neutron_pwd@controller/neutron

openstack-config --set /etc/neutron/neutron.conf DEFAULT auth_strategy keystone
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_host controller
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_protocol http
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_port 35357
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken admin_tenant_name service
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken admin_user neutron_user
openstack-config --set /etc/neutron/neutron.conf keystone_authtoken admin_password neutron_pwd

openstack-config --set /etc/neutron/neutron.conf DEFAULT rpc_backend neutron.openstack.common.rpc.impl_kombu
openstack-config --set /etc/neutron/neutron.conf DEFAULT rabbit_host controller
openstack-config --set /etc/neutron/neutron.conf DEFAULT rabbit_password rabbitmq_pwd

openstack-config --set /etc/neutron/neutron.conf DEFAULT notify_nova_on_port_status_changes True
openstack-config --set /etc/neutron/neutron.conf DEFAULT notify_nova_on_port_data_changes True
openstack-config --set /etc/neutron/neutron.conf DEFAULT nova_url http://controller:8774/v2


openstack-config --set /etc/neutron/neutron.conf DEFAULT nova_admin_username nova_user
openstack-config --set /etc/neutron/neutron.conf DEFAULT nova_admin_tenant_id $(keystone tenant-list | awk '/ service / { print $2 }')
openstack-config --set /etc/neutron/neutron.conf DEFAULT nova_admin_password nova_pwd
openstack-config --set /etc/neutron/neutron.conf DEFAULT nova_admin_auth_url http://controller:35357/v2.0

ln -s /etc/neutron/plugins/ml2/ml2_conf.ini /etc/neutron/plugin.ini


openstack-config --set /etc/neutron/neutron.conf DEFAULT core_plugin ml2
openstack-config --set /etc/neutron/neutron.conf DEFAULT service_plugins router


openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 type_drivers gre
openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types gre
openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 mechanism_drivers openvswitch
openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_gre tunnel_id_ranges 1:1000
openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini securitygroup firewall_driver neutron.agent.linux.iptables_firewall.OVSHybridIptablesFirewallDriver
openstack-config --set /etc/neutron/plugins/ml2/ml2_conf.ini securitygroup enable_security_group true

openstack-config --set /etc/nova/nova.conf DEFAULT network_api_class nova.network.neutronv2.api.API
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_url http://controller:9696
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_auth_strategy keystone
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_admin_tenant_name service
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_admin_username neutron_user
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_admin_password neutron_pwd
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_admin_auth_url http://controller:35357/v2.0
openstack-config --set /etc/nova/nova.conf DEFAULT linuxnet_interface_driver nova.network.linux_net.LinuxOVSInterfaceDriver
openstack-config --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver
openstack-config --set /etc/nova/nova.conf DEFAULT security_group_api neutron


openstack-config --set /etc/nova/nova.conf DEFAULT service_neutron_metadata_proxy true
openstack-config --set /etc/nova/nova.conf DEFAULT neutron_metadata_proxy_shared_secret METADATA_SECRET

su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neutron.conf \
  --config-file /etc/neutron/plugins/ml2/ml2_conf.ini upgrade juno" neutron

systemctl restart openstack-nova-api openstack-nova-scheduler openstack-nova-conductor

systemctl enable neutron-server.service
systemctl start neutron-server.service
systemctl restart openstack-nova-api.service openstack-nova-scheduler.service \
openstack-nova-conductor.service

#############################cinder######################################

openstack-config --set /etc/cinder/cinder.conf database connection mysql://mysql_cinder_user:mysql_cinder_pwd@controller/cinder

mysql -uroot -pmysql_root_pwd -e  "CREATE DATABASE cinder;"
mysql -uroot -pmysql_root_pwd -e  "GRANT ALL PRIVILEGES ON cinder.* TO 'mysql_cinder_user'@'localhost' IDENTIFIED BY 'mysql_cinder_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON cinder.* TO 'mysql_cinder_user'@'%' IDENTIFIED BY 'mysql_cinder_pwd';"
mysql -uroot -pmysql_root_pwd -e "GRANT ALL PRIVILEGES ON cinder.* TO 'mysql_cinder_user'@'controller' IDENTIFIED BY 'mysql_cinder_pwd';"

su -s /bin/sh -c "cinder-manage db sync" cinder

keystone user-create --name=cinder_user --pass=cinder_pwd --email=cinder_email
keystone user-role-add --user=cinder_user --tenant=service_tenant --role=admin_role

keystone service-create --name=cinder --type=volume --description="OpenStack Block Storage"

keystone endpoint-create \
--service-id $(keystone service-list | awk '/ volume / {print $2}') \
--publicurl http://controller:8776/v1/%\(tenant_id\)s \
--internalurl http://controller:8776/v1/%\(tenant_id\)s \
--adminurl http://controller:8776/v1/%\(tenant_id\)s \
--region regionOne

keystone service-create --name=cinderv2 --type=volumev2 --description="OpenStack Block Storage v2"

keystone endpoint-create \
--service-id $(keystone service-list | awk '/ volumev2 / {print $2}') \
--publicurl http://controller:8776/v2/%\(tenant_id\)s \
--internalurl http://controller:8776/v2/%\(tenant_id\)s \
--adminurl http://controller:8776/v2/%\(tenant_id\)s \
--region regionOne

openstack-config --set /etc/cinder/cinder.conf DEFAULT auth_strategy keystone
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_host controller
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_protocol http
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_port 35357
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken admin_user cinder_user
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken admin_tenant_name service
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken admin_password cinder_pwd

openstack-config --set /etc/cinder/cinder.conf DEFAULT rpc_backend cinder.openstack.common.rpc.impl_kombu
openstack-config --set /etc/cinder/cinder.conf DEFAULT rabbit_host controller
openstack-config --set /etc/cinder/cinder.conf DEFAULT rabbit_password rabbitmq_pwd

systemctl enable openstack-cinder-api.service openstack-cinder-scheduler.service
systemctl start openstack-cinder-api.service openstack-cinder-scheduler.service

systemctl enable openstack-cinder-api.service openstack-cinder-scheduler.service
systemctl start openstack-cinder-api.service openstack-cinder-scheduler.service